package com.example.demo.service;

import com.example.demo.dao.StudentDao;
import com.example.demo.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StudentService {
    @Autowired
    private StudentDao studentDao;
    public List<Student> list(Student student){
        List<Student> sts;
        if(student.getName()==""&&student.getAge()!=null){
            sts = studentDao.findByAge(student.getAge());
        }
        else{
            sts = studentDao.findAll(Example.of(student));
        }
        return sts;
    }
    public void deleteById(Long id) {
        studentDao.deleteById(id);
    }

    public void save(Student st) {
        studentDao.save(st);
    }
    public Student getbyid(Long id) {
        Student st;
        try{
            st =  studentDao.getReferenceById(id);
        }
        catch (Exception e){
            st = null;
        }
        return st;
    }
}
